﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using StockMatch.Service;
using StockMatch.Service.Entity;
using Titan;

namespace StockMatch.WebSite.Areas.Admin.Controllers
{
    /// <summary>
    /// 对于方案的管理
    /// </summary>
    public class StockController : AdminControllerBase
    {
        public ActionResult Index(int? page, string kw)
        {
            ViewBag.Menu = "Stock";
            ViewBag.Tag = "Index";

            int pageIndex = (page == null ? 1 : (int) page);

            #region 查询列表

            var query = new QueryExpression();
            query.EntityType = typeof (Project);
            query.Select(Project_.ALL);
            query.Select(Project_.User.ALL);
            if (!string.IsNullOrEmpty(kw))
            {

            }
            query.OrderBys.Add(Project_.ProjectId.Desc);
            query.PageIndex = pageIndex;
            query.PageSize = 5;
            query.ReturnMatchedCount = true;
            var projects = new Projects();
            projects = ServiceManager.ProjectService.GetProjects(query);

            #endregion
            ViewBag.TotalCount = projects.TotalCount;
            ViewBag.PageIndex = pageIndex;
            ViewBag.Keyword = kw;
            return View(projects);
        }

        public ActionResult RiskMargin(int? page, string kw)
        {
            ViewBag.Menu = "Stock";
            ViewBag.Tag = "RiskMargin";
            int pageIndex = page == null ? 1 : (int)page;

            #region 查询列表

            var query = new QueryExpression();
            query.EntityType = typeof(ProjectPrincipal);
            query.Select(ProjectPrincipal_.ALL);
            query.Select(ProjectPrincipal_.Project.ProjectNumber);
            if (!string.IsNullOrEmpty(kw))
            {

            }
            query.OrderBys.Add(ProjectPrincipal_.ProjectId.Desc);
            query.PageIndex = pageIndex;
            query.PageSize = 10;
            query.ReturnMatchedCount = true;
            ProjectPrincipals projectPrincipals = new ProjectPrincipals();
            projectPrincipals = ServiceManager.ProjectPrincipalService.GetProjectPrincipals(query);

            #endregion

            ViewBag.PageIndex = pageIndex;
            ViewBag.TotalCount = projectPrincipals.TotalCount;
            ViewBag.Keyword = kw;
            return View(projectPrincipals);
        }

        /// <summary>
        /// 验资通过
        /// </summary>
        /// <param name="id"></param>
        /// <param name="page"></param>
        /// <param name="kw"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult CapitalVerification(int id)
        {
            Project project = new Project
            {
                ProjectId = id
            };

            if (ServiceManager.ProjectService.GetProject(id).ProjectState >=
                (int)ProjectStateEnum.OpenAccount)
            {
                return Json(new { data = "不能重复验资" });
            }

            project.ProjectState = (int)ProjectStateEnum.OpenAccount;
            ServiceManager.ProjectService.Update(project, Project_.ProjectState);
            return Json(new { data = "验资通过" });
        }

        /// <summary>
        /// 开户
        /// </summary>
        /// <param name="id"></param>
        /// <param name="tradingAccount"></param>
        /// <param name="tradingPassword"></param>
        /// <returns></returns>
        public ActionResult OpenAccount(int id, string tradingAccount, string tradingPassword)
        {
            if (string.IsNullOrEmpty(tradingAccount))
            {
                return Json(new { data = "交易账号不能为空" });
            }
            if (string.IsNullOrEmpty(tradingPassword))
            {
                return Json(new { data = "交易密码不能为空" });
            }

            if (ServiceManager.ProjectService.GetProject(id).ProjectState >
                (int)ProjectStateEnum.OpenAccount)
            {
                return Json(new { data = "已设置过交易账号" });
            }

            if (ServiceManager.ProjectService.OpenAccount(id, tradingAccount, tradingPassword))
            {
                return Json(new { data = "设置交易账号成功" });
            }
            return Json(new { data = "设置交易账号失败" });
        }

        /// <summary>
        /// 强制终止
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Termination(int id)
        {
            Project Project = new Project
            {
                ProjectId = id
            };
            Project.ProjectState = (int)ProjectStateEnum.Termination;
            if (ServiceManager.ProjectService.Update(Project, Project_.ProjectState))
            {
                return Json(new { data = "终止操盘成功" });
            }
            return Json(new { data = "终止操盘失败" });
        }


    }
}
